using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class MasseTutorial : MonoBehaviour
{
	private Animator m_Animator;

	[SerializeField]
	private SkeletonAnimation m_SpineAnimation;

	private Image m_Pointer;

	[SerializeField]
	private RectTransform m_SliderSimulation;

	[SerializeField]
	private RectTransform m_DragArea;

	private Transform m_RefBall;

	[SerializeField]
	private Text[] m_Texts;

	private Vector3 m_PointerDir;

	private int m_Step;

	private float m_Time;

	private bool m_Drag;

	private Vector2 m_FinalPosition = new Vector2(4.4f, -32.2f);

	private void Awake()
	{
		m_Animator = GetComponentInChildren<Animator>();
		m_Step = 0;
		m_SpineAnimation.UpdateComplete += SpineIdle1PlayComplete;
		m_Texts[0].canvasRenderer.SetAlpha(0.01f);
		m_Texts[1].canvasRenderer.SetAlpha(0.01f);
		m_DragArea.gameObject.SetActive(value: false);
		float num = -999f;
		PoolBall[] customBallsArray = Pools.CustomBallsArray;
		int i = 0;
		for (int num2 = customBallsArray.Length; i < num2; i++)
		{
			Vector3 position = customBallsArray[i].transform.position;
			if (position.z > num)
			{
				m_RefBall = customBallsArray[i].transform;
			}
		}
		base.name = "Tutorial";
	}

	private void Start()
	{
		m_SliderSimulation.transform.position = BaseUIController.fireSlider.transform.position;
		m_Texts[0].CrossFadeAlpha(1f, 0.2f, ignoreTimeScale: false);
		m_Texts[0].text = HOLocalizationConfiguration.GetValue(140);
		Next();
	}

	private void SpineIdle1PlayComplete(SkeletonRenderer skeletonRenderer)
	{
		m_SpineAnimation.UpdateComplete -= SpineIdle1PlayComplete;
		m_SpineAnimation.AnimationName = "Idle2";
		m_SpineAnimation.loop = true;
	}

	public void OnPointClick(BaseEventData data)
	{
		AnimatorStateInfo currentAnimatorStateInfo = m_Animator.GetCurrentAnimatorStateInfo(0);
		if (currentAnimatorStateInfo.IsName("Click"))
		{
			BaseUIController.cueOperateArea.pointerOperation.PointerAtWorld(m_RefBall.position);
			Next();
		}
		else if (currentAnimatorStateInfo.IsName("Click2"))
		{
			BaseUIController.siding.GetComponent<Animator>().SetTrigger("Show");
			Next();
		}
	}

	public void OnPointerDragBegin(BaseEventData data)
	{
		m_Drag = true;
	}

	public void OnPointerDrag(BaseEventData data)
	{
		if (!m_Drag)
		{
			return;
		}
		AnimatorStateInfo currentAnimatorStateInfo = m_Animator.GetCurrentAnimatorStateInfo(0);
		if (currentAnimatorStateInfo.IsName("MasseShot") && m_Step == 3)
		{
			Vector2 v = BaseUIController.GetUICamera().ScreenToWorldPoint(((PointerEventData)data).position);
			Vector2 v2 = BaseUIController.siding.sideBallO.worldToLocalMatrix.MultiplyPoint(v);
			v2.y = Mathf.Clamp(v2.y, m_FinalPosition.y, 0f);
			v2.x = v2.y / m_FinalPosition.y * m_FinalPosition.x;
			BaseUIController.siding.sidingAnchor.SetTargetPosition(v2);
			Vector2 sidingOffset = BaseUIController.siding.sidingOffset;
			if (sidingOffset.y == -1f)
			{
				Next();
			}
		}
		else if (currentAnimatorStateInfo.IsName("English") && m_Step == 4)
		{
			BaseUIController.siding.OnCueDrag(data);
			UnityEngine.Debug.Log("drag angle:" + BaseUIController.siding.MasseAngle);
			if (BaseUIController.siding.MasseAngle > 64)
			{
				BaseUIController.siding.MasseAngle = 64;
			}
			if ((float)BaseUIController.siding.MasseAngle == 64f)
			{
				Next();
				BaseUIController.siding.GetComponent<Animator>().SetTrigger("Hide");
			}
		}
	}

	public void OnSliderDrag(BaseEventData data)
	{
		if (m_Animator.GetCurrentAnimatorStateInfo(0).IsName("DragPower"))
		{
			BaseUIController.fireSlider.OnFireSliderDrag(data);
		}
	}

	public void OnSliderDragEnd(BaseEventData data)
	{
		if (m_Animator.GetCurrentAnimatorStateInfo(0).IsName("DragPower") && BaseUIController.fireSlider.lastValue > 0.02f)
		{
			BaseUIController.fireSlider.OnFireSliderDragEnd(data);
			Pools.Cue.SetPowerScalar(1f);
			Next();
		}
	}

	public void Next()
	{
		m_Step++;
		m_Animator.SetInteger("TutorialStep2", m_Step);
		m_Drag = false;
		int num = m_Step % 2;
		m_Texts[num].text = HOLocalizationConfiguration.GetValue(143 + m_Step - 1);
		m_Texts[num].CrossFadeAlpha(1f, 0.2f, ignoreTimeScale: false);
		m_Texts[1 - num].CrossFadeAlpha(0f, 0.2f, ignoreTimeScale: false);
		if (m_Step == 1)
		{
			Transform transform = null;
			float num2 = -999f;
			PoolBall[] customBallsArray = Pools.CustomBallsArray;
			int i = 0;
			for (int num3 = customBallsArray.Length; i < num3; i++)
			{
				Vector3 position = customBallsArray[i].transform.position;
				if (position.z > num2)
				{
					transform = customBallsArray[i].transform;
				}
			}
			m_Animator.transform.position = MathTools.World2UI(transform.position);
		}
		else if (m_Step == 2)
		{
			m_Animator.transform.position = BaseUIController.siding.transform.Find("Toggle").position;
		}
		else if (m_Step == 3)
		{
			m_Animator.transform.position = BaseUIController.siding.anchorO.position;
			m_DragArea.gameObject.SetActive(value: true);
			m_DragArea.position = BaseUIController.siding.anchorO.parent.position;
			m_DragArea.sizeDelta = BaseUIController.siding.anchorO.parent.GetComponent<RectTransform>().sizeDelta * 4f;
		}
		else if (m_Step == 4)
		{
			Vector3 position2 = BaseUIController.siding.masseText.position;
			position2.x -= 0.5f;
			position2.y += 0.2f;
			m_Animator.transform.position = position2;
			m_DragArea.position = BaseUIController.siding.masseText.parent.parent.position;
			Vector2 sizeDelta = BaseUIController.siding.masseText.parent.parent.GetComponent<RectTransform>().sizeDelta;
			sizeDelta.x += 150f;
			sizeDelta.y += 150f;
			m_DragArea.sizeDelta = sizeDelta;
			m_DragArea.sizeDelta = new Vector2(850f, 850f);
		}
		else if (m_Step == 5)
		{
			Vector3 localPosition = BaseUIController.fireSlider.transform.localPosition;
			float y = localPosition.y;
			Vector2 sizeDelta2 = (BaseUIController.fireSlider.transform as RectTransform).sizeDelta;
			localPosition.y = y + (sizeDelta2.y * 0.5f - 30f);
			localPosition.x += 30f;
			m_Animator.transform.position = BaseUIController.fireSlider.transform.parent.TransformPoint(localPosition);
			m_DragArea.gameObject.SetActive(value: false);
		}
		else if (m_Step > 5)
		{
			UnityEngine.Object.Destroy(base.gameObject);
		}
	}
}
